package com.baibei.order.bill.deliveryquery;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.baibei.model.DeliveryQueryInfo;
import com.baibei.module.LazyLoadFragment;
import com.baibei.order.R;
import com.baibei.order.R2;
import com.baibei.ui.AppUI;
import com.baibei.widget.CommonRefreshLayout;

import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 补款取货记录
 */
public class DeliveryQueryFragment extends LazyLoadFragment implements DeliveryQueryContract.DeliveryQueryView {

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    DeliveryQueryAdapter mAdapter;
    @BindView(R2.id.refresh_layout)
    CommonRefreshLayout mRefreshLayout;

    /**
     * 表现层
     */
    private DeliveryQueryContract.Presenter mPresenter;

    public DeliveryQueryFragment() {
    }

    public static DeliveryQueryFragment newInstance() {
        DeliveryQueryFragment fragment = new DeliveryQueryFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = inject(DeliveryQueryContract.Presenter.class);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new DeliveryQueryAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                mPresenter.start();
            }
        });
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_expenditure_records;
    }


    @Override
    public void lazyLoad() {
        mRefreshLayout.autoRefresh();
    }


    @Override
    public void hideRefreshLayout() {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.refreshComplete();
        }
    }

    @Override
    public void onLoadFailed(String msg) {
        AppUI.failed(getContext(), msg);
    }

    @Override
    public void onLoadSuccess(List<DeliveryQueryInfo> list) {
        mAdapter.setInfos(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        if (mPresenter != null) {
            mPresenter.destroy();
        }
        super.onDestroy();
    }

}
